<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class member_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    function index()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $username = isset($_GET['username']) ? $_GET['username'] : false;
        $wsql = '';
        if ($username) {
            $wsql .= " (mobile = '" . $username . "' or nick = '" . $username . "') and ";
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . $wsql . ' ' . trim($wsql, ' and ');
        }
        $uall = $this->db->get_all("select * from " . table('member') . $wsql . " order by uid desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($uall) {
            foreach ($uall as $k => $v) {
                $v['last_login_time'] = _date($v['last_login_time']);
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(uid) as total from " . table('member'));
            $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            $this->assign('data', $data);
        }
        $this->display();
    }
    function enter()
    {
        $uid = isset($_GET['uid']) ? intval($_GET['uid']) : exit('用户ID不能为空！');
        $row = $this->db->get_one("select * from " . table('member') . " where uid = " . $uid);
        if ($row['uid']) {
            $this->module('user')->user_session($row);
            location('member.php');
        } else {
            exit('用户不存在');
        }
    }
    public function edit()
    {
        $uid = !empty($_GET['uid']) ? intval($_GET['uid']) : false;
        if ($uid) {
            $row = $this->db->get_one("select * from " . table('member') . " where uid = " . $uid);
            if ($row['uid']) {
                $this->assign('row', $row);
            } else {
                error_404('该用户不存在');
            }
        }
        $this->display();
    }
    function edit_save()
    {
        if ($_POST) {
            $uid = P('uid', false);
            $mobile = P('mobile', '电话不能为空');
            $password = P('password');
            $repasswd = P('repasswd');
            if ($password != $repasswd) {
                error_json('两次密码不一致');
            }
            $cksql = '';
            if ($uid) {
                $cksql = ' and uid != ' . $uid . ' ';
            }
            $email = isset($_POST['email']) ? trim($_POST['email']) : '';
            $check_email = $this->db->get_one("select uid,email from " . table('member') . " where email = '" . $email . "' " . $cksql . " limit 1");
            if (!empty($check_email['uid'])) {
                error_json('邮箱已经纯在');
            }
            $check_mobile = $this->db->get_one("select uid,mobile from " . table('member') . " where mobile = '" . $mobile . "' " . $cksql . " limit 1");
            if (!empty($check_mobile['uid'])) {
                error_json('电话号码已经纯在');
            }
            if ($uid) {
                if (!empty($password)) {
                    $check_row = $this->db->get_one("select uid,rand from " . table('member') . " where uid = " . $uid);
                    $setsql['password'] = uqcms_password_hash($password, $check_row['rand']);
                }
            } else {
                if (!empty($password)) {
                    $rand = get_rand(4);
                    $setsql['rand'] = $rand;
                    $setsql['password'] = uqcms_password_hash($password, $rand);
                    $setsql['register_ip'] = get_ip();
                    $setsql['last_login_ip'] = get_ip();
                    $setsql['addtime'] = time();
                } else {
                    error_json('密码设置不能为空');
                }
            }
            $setsql['nick'] = P('nick');
            $setsql['email'] = $email;
            $setsql['mobile'] = $mobile;
            $setsql['status'] = P('status', '', '0');
            if ($uid) {
                $query = $this->db->update(table('member'), $setsql, "uid = " . $uid);
            } else {
                $query = $this->db->add(table('member'), $setsql);
                $uid = $this->db->insert_id();
                $create_shop_status = P("create_shop", '', '0');
                if ($create_shop_status == '1') {
                    $sparr['uid'] = $uid;
                    $sparr['name'] = P('spname');
                    $sparr['addtime'] = time();
                    $this->db->add(table('shop'), $sparr);
                    $this->db->query("update " . table('member') . " set type = 2 where uid = " . $uid);
                }
            }
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function del()
    {
        $uid = isset($_POST['id']) ? $_POST['id'] : error_json('用户UID提交错误');
        $this->db->autocommit();
        $sprow = $this->db->get_one("select id,uid from " . table('shop') . " where uid = " . $uid);
        if (!$sprow['id']) {
            $this->db->delete(table('member'), 'uid = ' . $uid);
            $this->db->delete(table('member_withdraw'), 'uid = ' . $uid);
            $this->db->delete(table('member_card'), 'uid = ' . $uid);
            $this->db->delete(table('member_bill'), 'uid = ' . $uid);
            $this->db->delete(table('member_address'), 'uid = ' . $uid);
            $this->db->delete(table('admin_pay'), 'uid = ' . $uid);
            $this->db->delete(table('collect_goods'), 'uid = ' . $uid);
            $this->db->delete(table('collect_shop'), 'uid = ' . $uid);
            $this->db->delete(table('distribute'), 'uid = ' . $uid);
            $this->db->delete(table('distribute_bill'), 'uid = ' . $uid);
            $this->db->delete(table('distribute_relate'), 'uid = ' . $uid);
            $this->db->delete(table('distribute_settle'), 'uid = ' . $uid);
            $cquery = $this->db->get_all("select * from " . table('goods_comment') . " where uid = " . $uid);
            if ($cquery) {
                foreach ($cquery as $v) {
                    $this->db->delete(table('goods_comment_img'), 'comment_id = ' . $v['id']);
                }
                $this->db->delete(table('goods_comment'), 'uid = ' . $uid);
            }
            $this->db->update(table('album'), array('status' => '0'), 'uid = ' . $uid);
            $this->db->delete(table('notice'), 'uid = ' . $uid);
            $oquery = $this->db->get_all("select * from " . table('order_info') . " where uid = " . $uid);
            if ($oquery) {
                foreach ($oquery as $v) {
                    $this->db->delete(table('order_goods'), 'oid = ' . $v['id']);
                    $this->db->delete(table('order_address'), 'oid = ' . $v['id']);
                }
                $this->db->delete(table('order_info'), 'uid = ' . $v['id']);
            }
            $this->db->delete(table('order_score'), 'uid = ' . $uid);
            $rquery = $this->db->get_all("select * from " . table('refund') . " where uid = " . $uid);
            if ($rquery) {
                foreach ($rquery as $v) {
                    $this->db->delete(table('refund_process'), 'rid = ' . $v['id']);
                }
                $this->db->delete(table('refund'), 'uid = ' . $uid);
            }
            $query = true;
            if ($query) {
                $this->db->commit();
                right_json();
            } else {
                $this->db->rollback();
                error_json('删除失败');
            }
        } else {
            error_json('请先删除商户，再删除会员');
        }
    }
    public function type()
    {
        $data = $this->db->get_all("select * from " . table('member_type') . " order by id desc");
        $this->assign('data', $data);
        $this->display();
    }
    function type_edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('member_type') . " where id = " . $id);
            $this->assign('row', $row);
        }
        $this->display();
    }
    function type_save()
    {
        $data['name'] = P('name', '等级名称不能为空');
        $data['integral'] = P('integral', '', '0');
        $id = P('id', 0);
        if ($id) {
            $query = $this->db->update(table('member_type'), $data, 'id = ' . $id);
        } else {
            $query = $this->db->add(table('member_type'), $data);
        }
        if ($query) {
            right_json();
        } else {
            error_json('保存失败');
        }
    }
    function type_del()
    {
        $id = P('id', '提交错误');
        $query = $this->db->delete(table('member_type'), 'id = ' . $id);
        if ($query) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
}